﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;

namespace NeoReflow
{
    public class CtrlCollectHelper
    {
        public static void GetMyCtrlList<T>(UIElementCollection uiControls, ref List<T> list) where T : UIElement
        {
            try
            {
                foreach (UIElement element in uiControls)
                {
                    if (element is T)
                    {
                        list.Add(element as T);
                    }


                    else if (element is Grid)
                    {
                        GetMyCtrlList<T>((element as Grid).Children, ref list);
                    }
                    else if (element is Canvas)
                    {
                        GetMyCtrlList<T>((element as Canvas).Children, ref list);
                    }

                    else if (element is Expander)
                    {
                        if ((element as Expander).Content is StackPanel)
                        {
                            StackPanel sa = (element as Expander).Content as StackPanel;
                            GetMyCtrlList<T>(sa.Children, ref list);
                        }
                        else if ((element as Expander).Content is Grid)
                        {
                            Grid sa = (element as Expander).Content as Grid;
                            GetMyCtrlList<T>(sa.Children, ref list);
                        }
                    }
                    else if (element is StackPanel)
                    {
                        GetMyCtrlList<T>((element as StackPanel).Children, ref list);
                    }
                    else if (element is Border)
                    {
                        if ((element as Border).Child is StackPanel)
                        {
                            StackPanel sp = (element as Border).Child as StackPanel;
                            GetMyCtrlList<T>(sp.Children, ref list);
                        }
                        else if ((element as Border).Child is Canvas)
                        {
                            Canvas sp = (element as Border).Child as Canvas;
                            GetMyCtrlList<T>(sp.Children, ref list);
                        }

                    }

                    else if (element is TabControl)
                    {
                        GetMyTabCtrlList<T>(element as TabControl, ref list);
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        public static void GetMyTabCtrlList<T>(TabControl uiControls, ref List<T> list) where T : UIElement
        {
            try
            {
                //遍历TabControl
                var temp = uiControls.Items;
                //var temp = this.tabControl1.Items;
                foreach (TabItem item in temp)
                {
                    if (item.Content is T)
                    {
                        list.Add(item.Content as T);
                    }


                    else if (item.Content is Grid)
                    {
                        //若TabControl中嵌套grid
                        GetMyCtrlList<T>((item.Content as Grid).Children, ref list);

                    }
                    else if (item.Content is Canvas)
                    {
                        //若TabControl中嵌套grid
                        GetMyCtrlList<T>((item.Content as Canvas).Children, ref list);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
